package Queue;

public class MyLinkedListQueue {

    Node head = new Node(-1,null);
    int size = 0;
    class Node{
        int data;
        Node next;

        public Node(int data, Node next) {
            this.data = data;
            this.next = next;
        }
    }

    //入队列操作,可以尾插处理
    public void enQueue(int data){
        if (head.next == null){
            head.next = new Node(data,null);
            size++;
            System.out.println("入队列操作成功");
            return;
        }
        Node p = head.next;
        while (p.next != null){
            p = p.next;
        }
        p.next = new Node(data,null);
        size++;
        System.out.println("入队列成功");

    }

    //出队列操作，可以头删处理
    public int deQueue(){
        if (size == 0){
            //意味着只有一个头节点，就没有必要删除了
            System.out.println("出队列失败");
            return -1;
        }

        Node temp = head.next;
        head.next = temp.next;
        size--;
        System.out.println("出队列操作成功");
        return temp.data;
    }

    //查看队头元素
    public int peek(){
        if (size == 0){
            System.out.println("查看队头元素失败");
        }
        return head.next.data;
    }

    //输出所有队列元素
    public void printAll(){
        Node p = head;
        while (p.next != null){
            p = p.next;
            System.out.print(p.data + " ");
        }
    }

}
